getApp();

var api = require("../../utils/ble/bleApi.js"),
  md5 = require("../../utils/md5Util.js"),
  util = (require("../../utils/ble/bleUtil.js"),
    require("../../utils/strUtil.js"));

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    bindPasswd: "",
    hiddenmodalputBindBlt: "none",
    hiddenmodalputUnlockBlt: "none",
    DevicesConnected: [],
    //存储计时器
    setInter: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log('--------onLoad--------')

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // console.log('--------onReady--------')

  },

  /**
   * 发送指令绑定指定name的BLE设备
   */
  bindUnlockUnbind: function() {
    wx.showLoading({
      title: '搜索中...',
      // mask: true
    })
    var _this = this;
    api.sendCommand({
      name: _this.data.name,
      sendCommend: "11" + util.stringToHex(_this.data.bindPasswd) + "90",
      onSuccessCallBack: function(t) {
        var _current = wx.getStorageSync("currentDevice"),
          _connected = wx.getStorageSync("DevicesConnected");

        /**
         * 发送指令解锁
         */
        console.log('等待12s开始发送开锁指令')
        setTimeout(function() {
          wx.hideLoading();
          _this.unlock();
          wx.showLoading({
            title: '开锁中...',
            // mask: true
          })
        }, 12000);
        _this.data.setInter = setInterval(function() {
          console.log('等待+1s')
        }, 1000);

        for (var i in _connected) {
          if (_current.deviceId == _connected[i].deviceId) return;
          "" == _connected && (_connected = [])
          _connected.push({
            deviceId: _current.deviceId,
            name: _current.name,
            passwd: _this.data.bindPasswd
          })
          wx.setStorageSync("DevicesConnected", _connected)
          console.log("DevicesConnected", _connected)
          _this.setData({
            DevicesConnected: _connected
          });
        }
      },
      onFailCallBack: function(t) {
        wx.showModal({
          title: "错误",
          content: t,
        });
        wx.hideLoading();
      }
    })
  },

  /**
   * 执行开锁指令 
   */
  unlock: function(s) {
    var _this = this,
      _rand = parseInt(10 + 80 * Math.random()),
      _command = "23" + _rand + md5.hexMD5(_rand + _this.data.bindPasswd).substring(24) + "23";
    clearInterval(_this.data.setInter)
    console.log(_command), api.sendCommand({
      name: _this.data.name,
      sendCommend: _command,
      onSuccessCallBack: function(o) {
        console.log(o);

        /**
         * 发送指令解绑
         */
        console.log('等待12s开始发送解绑指令')
        _this.data.setInter = setInterval(function() {
          console.log('等待+1s')
        }, 1000);
        setTimeout(function() {
          _this.unbind();
        }, 12000);
      },
      onFailCallBack: function(o) {
        console.log(o);
      }
    });
  },

  /**
   * 发送指令解绑
   */
  unbind: function(s) {
    console.log('发送指令解绑')
    var _this = this,
      _rand = parseInt(10 + 80 * Math.random()),
      _command = "35" + _rand + md5.hexMD5(_rand + _this.data.bindPasswd).substring(24) + "35";
    clearInterval(_this.data.setInter)
    console.log(_command), api.sendCommand({
      name: _this.data.name,
      sendCommend: _command,
      onSuccessCallBack: function(o) {
        console.log(o)
        wx.clearStorageSync("DevicesConnected")
        wx.hideLoading()
        wx.showToast({
          title: '操作成功',
          icon: "",
          image: "",
          duration: 1500
        })
      },
      onFailCallBack: function(o) {
        console.log(o);
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // console.log('--------onShow--------')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    // console.log('--------onHide--------')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // console.log('--------onUnload--------')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // console.log('--------onPullDownRefresh--------')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})